<template>
  <div class="main">
    <el-card class="search-box">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <el-form-item label="商品名称" prop="goodsName">
          <el-input
            v-model="queryParams.goodsName"
            placeholder="请输入商品名称"
            clearable
            @keyup.enter.native="getGoodsList"
          />
        </el-form-item>
        <el-form-item label="状态" prop="isSale">
          <el-select v-model="queryParams.isSale" placeholder="请选择" clearable>
            <el-option label="上架" :value="1" />
            <el-option label="下架" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="time">
          <el-date-picker
            v-model="value"
            format="yyyy-MM-dd HH:mm:ss" 
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            @change="selectTime"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getGoodsList"
            >查询</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-box">
      <el-button
        type="primary"
        class="el-icon-plus"
        size="mini"
        @click="addGoods()"
        >新增商品</el-button
      >
      <el-table
        :data="tableData"
        :header-cell-style="{
          background: '#f5f7fa',
          color: '#000000',
        }"
        style="width: 100%"
      >
        <el-table-column prop="goodsName"  width="280" label="商品描述">
          <template slot-scope="scope">
            <div class="msg">
              <img :src="scope.row.goodsThumb" alt="">
              <p>{{scope.row.goodsName}}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="parentCategoryName" label="分类"> </el-table-column>
        <el-table-column prop="defaultSalePrice" label="商品售价"> </el-table-column>
        <el-table-column prop="defaultOriginalPrice" label="划线价"> </el-table-column>
        <el-table-column prop="defaultCostPrice" label="成本价"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            <el-switch
              class="switchStyle"
              @change="updateDown(scope.row.isSale,scope.row.id)"
              v-model="scope.row.isSale"
              active-text="上架"
              inactive-text="下架"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column prop="address" label="操作"> 
          <template slot-scope="scope">
            <el-button  size="mini" type="primary" @click="handleUpdate(scope.row.id)">
              编辑
              <!-- {{scope.row.state}} -->
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNumber" :limit.sync="queryParams.pageSize" @pagination="getGoodsList" />
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisibleClose"
      width="30%"
      :before-close="handleDeleteClose">
      <span>确定进行删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleClose = false">取 消</el-button>
        <el-button type="primary" @click="submitDelete()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import {queryGoodsList,deleteGoods,updateGoodsDown} from '@/api/goods'
export default {
  name: "",
  components: { Pagination },
  props: {},
  data() {
    return {
      goodsId:null,
      dialogVisibleClose:false,
      queryParams: {
        pageNumber:1,
        pageSize:10,
        startTime:'',
        endTime:'',
        goodsName:'',
        isSale: null
      },
      status:1,
      total: 0,
      tableData: [
       
      ],
      value:''
    };
  },
  watch: {},
  computed: {},
  methods: {
    addGoods(){
      this.$router.push('/goods/add')
    },
    resetQuery() {
      this.queryParams= {
        pageNumber:1,
        pageSize:10,
        startTime:'',
        endTime:'',
        goodsName:'',
        isSale: null
      },
      this.getGoodsList()
    },
    async getGoodsList(){
      const res = await queryGoodsList(this.queryParams);
      this.tableData = res.data.list
      this.total = res.data.total
    },
    selectTime(val){
      this.queryParams.startTime = val[0]+''
      this.queryParams.endTime = val[1]+''
    },
    handleUpdate(goodsId){
      this.$router.push({path:'/goods/edit',query: {goodsId}})
    },
    handleDelete(id){
      this.goodsId = id
      this.dialogVisibleClose = true
    },
    handleDeleteClose(){
      this.dialogVisibleClose = false
    },
    submitDelete(){
      deleteGoods(this.goodsId).then(res=>{
        if(res.code == 200){
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.getGoodsList()
          this.dialogVisibleClose = false
        }
      })
    },
    updateDown(type,id){
      let params = {
        scene:type?'UP':'DOWN'
      }
      updateGoodsDown(params,id).then(res=>{
        this.$message({
                    message: res.message,
                    type: 'success'
                });
        this.getGoodsList()
      })
    }
  },
  created() {
    this.getGoodsList()
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
  .el-form-item{
    margin-right: 20px;
  }
  .table-box{

  }
  .el-table{
    margin-top: 20px;
  }
  ::v-deep .switchStyle .el-switch__label {
    position: absolute;
    display: none;
    color: #fff;
  }
  ::v-deep .switchStyle .el-switch__label--left {
    z-index: 9;
    left: 19px;
  }
  ::v-deep .switchStyle .el-switch__label--right {
    z-index: 9;
    left: -2px;
  }
  ::v-deep .el-switch__label.is-active {
    display: block;
  }
  ::v-deep .el-switch .el-switch__core,
  .el-switch .el-switch__label {
    width: 60px !important;
  } 
  .msg{
    display: flex;
    align-items: center;
    img{
      margin-right: 10px;
      width: 80px;
    }
  }
</style>